////////////////////////////////////////////////////////////////////////////////////////////////////
// unshadowed, untextured phong for up to N positional/directional light sources
////////////////////////////////////////////////////////////////////////////////////////////////////

#ifdef GL_FRAGMENT_PRECISION_HIGH
	precision highp float;
#else
	precision mediump float;
#endif

const int num_lights = 1;

struct MaterialParameters {
	vec4 emission;		// Ecm
	vec4 ambient;		// Acm
	vec4 diffuse;		// Dcm
	vec4 specular;		// Scm
	vec4 shininess;		// Srm
};

uniform MaterialParameters material;

struct LightModelProducts {
	vec4 sceneColor;	// Ecm + Acm * Acs
};

uniform LightModelProducts lightModelProduct;

struct LightProducts {
	vec4 ambient;		// Acm * Acli
	vec4 diffuse;		// Dcm * Dcli
	vec4 specular;		// Scm * Scli
};

uniform LightProducts lightProduct[num_lights];

uniform samplerCube normalizer;

varying vec3 n_obj_i;				// surface normal vector in object space
varying vec3 l_obj_i[num_lights];	// light-source vector in object space, one per light
varying vec3 h_obj_i[num_lights];	// half-direction vector in object space, one per light

void main()
{
	vec3 n = textureCube(normalizer, n_obj_i).xyz * 2.0 - 1.0; 

	vec3 d = lightModelProduct.sceneColor.xyz;
	vec3 s = vec3(0.0);

	for (int i = 0; i < num_lights; ++i)
	{
		vec3 l = textureCube(normalizer, l_obj_i[i]).xyz * 2.0 - 1.0;
		vec3 h = textureCube(normalizer, h_obj_i[i]).xyz * 2.0 - 1.0;

		d += lightProduct[i].diffuse.xyz  * max(dot(n, l), 0.0);
		s += lightProduct[i].specular.xyz * pow(max(dot(n, h), 0.0), material.shininess.x);
	}

	gl_FragColor = vec4(d + s,
		lightModelProduct.sceneColor.w);
}
